package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import com.quanquan.management.dto.CommercialDTO;
import com.quanquan.management.dto.FloorDTO;
import com.quanquan.management.dto.StoreDTO;
import com.quanquan.management.request.FloorRequest;
import com.quanquan.management.request.StoreRequestBody;
import com.quanquan.management.service.CommercialService;
import com.quanquan.management.service.FloorService;
import com.quanquan.management.service.StoreService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import request.PageRequestBody;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gongzhou
 * @title: StoreController
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/3/289:21
 */
@Controller
@RequestMapping("/store")
public class StoreController extends ManagerBaseController {

    private final StoreService storeService;
    private final FloorService floorService;
    private final CommercialService commercialService;

    @Autowired
    public StoreController(StoreService storeService, FloorService floorService, CommercialService commercialService) {
        this.storeService = storeService;
        this.floorService = floorService;
        this.commercialService = commercialService;
    }

    /**
     * @param pageSize 商户列表
     * @param page
     * @param keyword
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        PageRequestBody requestBody = new PageRequestBody(page, pageSize, keyword, null, false);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        PageDTO<StoreDTO> pageDTO = storeService.listStore(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "store/list";
    }

    @GetMapping("/query")
    @ResponseBody
    public ResultBody query() {
        PageRequestBody requestBody = new PageRequestBody();
        requestBody.setPage(0);
        requestBody.setSize(999);
        PageDTO<StoreDTO> pageDTO = storeService.listStore(requestBody);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),pageDTO);
    }

    @GetMapping("/delete")
    @ResponseBody
    public ResultBody delete(@RequestParam String id) {
        boolean delete = storeService.delete(id);
        if (delete){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode());
        } else{
            return new ResultBody(ResCodeEnum.FAIL.getCode());
        }
    }

    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, StoreRequestBody requestBody, Model model) {

        List<CommercialDTO> commercialDTOs = commercialService.list();
        model.addAttribute("commercialDTOs",commercialDTOs);
        List<FloorDTO> floorDTOS = floorService.list();
        model.addAttribute("floorDTOs",floorDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            StoreDTO byId = storeService.findById(id);
            if(StringUtil.isEmpty(byId.getBanner())){
                byId.setBanner(null);
            }
            if(StringUtil.isEmpty(byId.getLogo())){
                byId.setLogo(null);
            }
            model.addAttribute("editable",byId);
            putFloors(model);
            return "store/form";
        }
        boolean update = storeService.update(requestBody);
        if (update) {
            return "redirect:/store/list";
        }
        return null;
    }

    /**
     * 添加
     * @param requestBody
     * @return
     */
    @RequestMapping("/add")
    public String add(StoreRequestBody requestBody,Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            List<CommercialDTO> commercialDTOs = commercialService.list();
            model.addAttribute("commercialDTOs",commercialDTOs);
            List<FloorDTO> floorDTOS = floorService.list();
            model.addAttribute("floorDTOs",floorDTOS);
            //putFloors(model);
            return "store/form";
        }
        boolean add = storeService.add(requestBody);
        if (add) {
            return "redirect:/store/list";
        }
        return null;
    }


    private void putFloors(Model model) {
        FloorRequest floorRequest = new FloorRequest();
        floorRequest.setStatusRemove(0);
        floorRequest.setPage(0);
        floorRequest.setSize(999);
        PageDTO<FloorDTO> floorDTOPageDTO = floorService.listAFloors(floorRequest);
        if (null != floorDTOPageDTO) {
            model.addAttribute("floors", floorDTOPageDTO.getList());
        } else {
            model.addAttribute("floors", new ArrayList<FloorDTO>());

        }
    }


}
